package com.wswy.wzcx.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

import com.che.libcommon.utils.RxBus;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.wswy.wzcx.model.DataCenter;
import com.wswy.wzcx.module.InternalMsgCode;
import com.wswy.wzcx.ui.data.PayResult;


/**
 * Created by wangduheng26 on 2017/8/7.
 * wangduheng26@gmail.com
 */

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    handleIntent();
  }

  private void handleIntent() {
    try {
      if (!DataCenter.get().getWXApi().handleIntent(getIntent(), this)) {
        finish();
      }
    } catch (Exception e) {
      e.printStackTrace();
      finish();
    }
  }

  @Override
  protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    handleIntent();
  }


  @Override
  public void onReq(BaseReq baseReq) {

  }

  @Override
  public void onResp(BaseResp resp) {
    if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//            Bundle bundle = new Bundle();
//            resp.toBundle(bundle);

      RxBus.getDefault().postWithCode(InternalMsgCode.CODE_PAY_RESULT, PayResult.buildFromWechat(resp.errCode));

      finish();
    }
  }
}
